<?php

/**
 * This is the model class for table "responsavel".
 *
 * The followings are the available columns in table 'responsavel':
 * @property integer $id
 * @property string $nome
 * @property string $estado_civil
 * @property string $sexo
 * @property string $data_de_nascimento
 * @property string $cpf
 * @property string $rg
 * @property string $profissao
 * @property string $telefone
 *
 * The followings are the available model relations:
 * @property Aluno[] $alunos
 */
class Responsavel extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'responsavel';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nome, estado_civil, sexo, data_de_nascimento, cpf, rg', 'required', 'message'=>'Campo {attribute} é obrigatório'),
			array('data_de_nascimento', 'EAgeValidator', 'message' => 'Responsável deve ter mais de 18 anos.',                    
                  'minAge'=>18, 
                  'maxAge'=>120, 
                  'allowEmpty'=>false
        	),
			array('nome, profissao', 'length', 'max'=>250),
			array('estado_civil, sexo, cpf, rg, telefone', 'length', 'max'=>45),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, nome, estado_civil, sexo, data_de_nascimento, cpf, rg, profissao, telefone', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'alunos' => array(self::HAS_MANY, 'Aluno', 'responsavel_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'nome' => 'Nome',
			'estado_civil' => 'Estado Civil',
			'sexo' => 'Sexo',
			'data_de_nascimento' => 'Data de Nascimento',
			'cpf' => 'Cpf',
			'rg' => 'Rg',
			'profissao' => 'Profissão',
			'telefone' => 'Telefone',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('nome',$this->nome,true);
		$criteria->compare('estado_civil',$this->estado_civil,true);
		$criteria->compare('sexo',$this->sexo,true);
		$criteria->compare('data_de_nascimento',$this->data_de_nascimento,true);
		$criteria->compare('cpf',$this->cpf,true);
		$criteria->compare('rg',$this->rg,true);
		$criteria->compare('profissao',$this->profissao,true);
		$criteria->compare('telefone',$this->telefone,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Responsavel the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
